package com.itheima.spring.bestpractive.common.myResult;

import lombok.Getter;

/**
 * @version 1.0
 * @author: HZY
 * @date: 2025/6/19 20:19
 * @Content:
 */
@Getter
public class R<T> {
    private final int code;
    private final String message;
    private final T data;

   private R(REnum rEnum, T data) {
        this.code = rEnum.getCode();
        this.message = rEnum.getMessage();
        this.data = data;
    }
    private R(int code, String message, T data){
        this.code = code;
        this.message = message;
        this.data = data;
    }
    // 成功：有数据
    public static <T> R<T> success(T data) {
        return new R<>(REnum.SUCCESS, data);
    }

    // 成功：无数据
    public static <T> R<T> success(){
        return new R<>(REnum.SUCCESS, null);
    }

    // 失败: 无数据
    public static <T> R<T> fail(REnum rEnum) {
        return new R<>(rEnum, null);
    }

    public static <T> R<T> fail(int code1,String message){
        return new R<>(code1, message,null);
    }

    // 失败：自定义信息
    public static <T> R<T> fail(String message) {
        return new R<>(REnum.FAIL.getCode(), message, null);
    }

    // 通用构造（用于自定义返回码）
    public static <T> R<T> of(REnum code, T data) {
        return new R<>(code, data);
    }


}
